import { Injectable } from '@nestjs/common';
import { TypeOrmCrudService } from '@nestjsx/crud-typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { PlayList } from '../entities';
import { CrudRequest } from '@nestjsx/crud';
import { plainToClass } from 'class-transformer';

@Injectable()
export class PlayListsService extends TypeOrmCrudService<PlayList> {
  constructor(@InjectRepository(PlayList) repo) {
    super(repo);
  }

  /**
   * Soft delete one entity.
   *
   * Override from the `Crud` `deleteOne` function to soft delete.
   * @param req `CrudRequest` from `Crud`
   */
  async deleteOne(req: CrudRequest): Promise<void | PlayList> {
    const { returnDeleted } = req.options.routes.deleteOneBase;
    const found = await this.getOneOrFail(req, returnDeleted);
    const toReturn = returnDeleted
      ? plainToClass(this.entityType, { ...found })
      : undefined;
    await this.repo.softRemove(found);
    return toReturn;
  }
}
